package cn.com.dashihui.web.controller;

import java.util.ArrayList;
import java.util.List;

import com.jfinal.plugin.activerecord.Record;

import cn.com.dashihui.web.base.BaseController;
import cn.com.dashihui.web.dao.GoodsSaleTemplate;
import cn.com.dashihui.web.service.GoodsSaleTemplateService;

/**
 * 平台基础商品管理，基础商品可由各分公司进行复制
 */
public class GoodsSaleTemplateController extends BaseController {
	private GoodsSaleTemplateService goodsTemplateService = new GoodsSaleTemplateService();
	/**
	 * 模板页面
	 */
	public void index(){
		//获取所有的模板
		List<GoodsSaleTemplate> list=goodsTemplateService.findAllGoodsTemplates();
		ArrayList<GoodsSaleTemplate> list2 = new ArrayList<>();
		//遍历集合，进行id和name的替换
		for (GoodsSaleTemplate goodsTemplate : list) {
			//关联商品类别的转换
		/*	String categorytwid = goodsTemplate.get("categorytwid");
			String categorytwname = null;
			if(categorytwid!=null){
				categorytwname = goodsTemplateService.findByCategorytwname(categorytwid);
			}else{
				return;
			}*/
			//关联商品规格的转换
			String specids = goodsTemplate.get("spec");
			String[] ids = specids.split(",");
			StringBuffer specNames = new StringBuffer();
			for (int i = 0; i < ids.length; i++) {
				int iid = Integer.parseInt(ids[i]);
				String specName = goodsTemplateService.findSpecByid(iid);
				specNames.append(specName + ",");
			}
			//进行替换
			GoodsSaleTemplate goodsTemplate2 = goodsTemplate.set("spec", specNames);
			list2.add(goodsTemplate2);
		}
		/*String sCategorytwid = 	goodsTemplateService.findByCategorytwname(sCategorytwid);*/
    	setAttr("list", list2);
    	System.out.println(list2);
        render("index.jsp");
    }
	/**
	 * 前往新增
	 */
	public void toAdd() {
		List<Record> listSpec = goodsTemplateService.findAllSpec();
		setAttr("listSpec", listSpec);
		render("add.jsp");
	}
	/**
	 * 模板新增
	 */
	public void doAdd() {
		String name = getPara("name");
		GoodsSaleTemplate findByName = goodsTemplateService.findByName(name);
		if (findByName != null) {
			renderResult(2);
			return;
		} else {
			//获取商品类别二级的id，以便查询出name
			/*String sCategoryonid = getPara("sCategoryonid");
			if (sCategoryonid==null) {
				renderResult(3);
				return;
			}
			String sCategorytwid = getPara("sCategorytwid");
			if (StrKit.isBlank(sCategorytwid)) {
				renderResult(4);
				return;
			}*/
			// 规格名称
			String[] specids = getParaValues("specids");
			StringBuffer sbids = new StringBuffer();
			for (String specName : specids) {
				sbids.append(specName+",");
			}
			String spec = sbids.toString();
			//模板名称
				GoodsSaleTemplate goodsTemplate = new GoodsSaleTemplate().set("name", name)
										.set("spec", spec)
										;
				goodsTemplateService.saveTemplate(goodsTemplate);
			}
			renderFailed();
	}
	/**
	 * 删除模板
	 */
	public void doDelete(){
		int id = getParaToInt(0,0);
		List<Record> saleGT = goodsTemplateService.findSaleGT(id);
		if (saleGT!=null && saleGT.size()>0) {
			renderResult(1);
			return;
		}
		if(id!=0&&goodsTemplateService.delete(id)){
			logger.info("删除规格【"+id+"】，并删除相应规格关联的参数信息");
			renderSuccess();
			return;
		}
		renderFailed();
	}
	/**
	 * 修改模板页面
	 */
	public void toEdit(){
		int id = getParaToInt(0,0);
		//根据前端id查询需要编辑的模板
		GoodsSaleTemplate Template = goodsTemplateService.findById(id);
		//关联商品规格的转换，直接返回到前端，以字符串的形式展示
		String specids = Template.get("spec");
		String[] ids = specids.split(",");
		StringBuffer specNames = new StringBuffer();
		for (int i = 0; i < ids.length; i++) {
			int iid = Integer.parseInt(ids[i]);
			String specName = goodsTemplateService.findSpecByid(iid);
			specNames.append(specName + ",");
		}
		Template.set("spec", specNames);
		setAttr("GoodsTemplate", Template);
		render("edit.jsp");
	}	
	/**
	 * 模板编辑
	 */
	public void doEdit() {
		//模板名称
		String name = getPara("name");	
		//模板ID
		int id = getParaToInt("id", 0);
		GoodsSaleTemplate goodsTemplate = goodsTemplateService.findById(id).set("name", name);
		goodsTemplateService.updateTemplate(goodsTemplate);
		renderFailed();
	}
	/**
	 * 详情页面
	 */
	public void toDetail() {
		int id = getParaToInt(0,0);
		//根据前端id查询需要编辑的模板
		GoodsSaleTemplate Template = goodsTemplateService.findById(id);
		//关联商品规格的转换，直接返回到前端，以字符串的形式展示
		String specids = Template.get("spec");
		String[] ids = specids.split(",");
		StringBuffer specNames = new StringBuffer();
		for (int i = 0; i < ids.length; i++) {
			int iid = Integer.parseInt(ids[i]);
			String specName = goodsTemplateService.findSpecByid(iid);
			specNames.append(specName + ",");
		}
		Template.set("spec", specNames);
		setAttr("GoodsTemplate", Template);
		render("templateDetail.jsp");
	}
}
